package com.samphanie.auiu.admin.controller;

import com.samphanie.auiu.admin.domain.dto.AllocResourceDto;
import com.samphanie.auiu.admin.service.ISysResourceService;
import com.samphanie.auiu.common.utils.ApiResponse;
import com.samphanie.auiu.mbg.model.SysResource;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.List;
import java.util.Map;


/**
 * 资源Controller
 *
 * @author zsy
 * @date 2021-05-18
 */
@RestController
@RequiredArgsConstructor
@RequestMapping("/admin/resource")
public class SysResourceController {
    private final ISysResourceService sysResourceService;

    /**
     * 查询资源列表
     */
    @GetMapping("/list")
    public ApiResponse<?> list(@RequestParam Map<String, Object> query) {
        return ApiResponse.success(sysResourceService.listPage(query));
    }

    @ApiOperation("查询所有资源列表")
    @GetMapping("/allList")
    public ApiResponse<?> allList() {
        return ApiResponse.success(sysResourceService.list());
    }

    /**
     * 获取资源详细信息
     */
    @GetMapping("/{id}")
    public ApiResponse<?> getInfo(@PathVariable("id") Long id) {
        return ApiResponse.success(sysResourceService.getById(id));
    }

    /**
     * 新增资源
     */
    @PostMapping("/add")
    public ApiResponse<?> add(@RequestBody SysResource sysResource) {
        sysResourceService.save(sysResource);
        return ApiResponse.success("新增成功");
    }

    /**
     * 修改资源
     */
    @PutMapping("/update")
    public ApiResponse<?> edit(@RequestBody SysResource sysResource) {
        sysResourceService.updateById(sysResource);
        return ApiResponse.success("修改成功");
    }

    /**
     * 批量修改资源
     */
    @PutMapping("/updateBatch")
    public ApiResponse<?> updateBatch(@RequestBody List<SysResource> resources) {
        sysResourceService.updateBatchById(resources);
        return ApiResponse.success("修改成功");
    }

    @ApiOperation("分配资源")
    @PostMapping("/allocResource")
    public ApiResponse<?> allocResource(@RequestBody AllocResourceDto allocResourceDto) {
        boolean res = sysResourceService.allocResource(allocResourceDto);
        return ApiResponse.success(res);
    }

    /**
     * 删除资源
     */
    @DeleteMapping("/delete")
    public ApiResponse<?> remove(@RequestBody Long[] ids) {
        sysResourceService.removeByIds(Arrays.asList(ids));
        return ApiResponse.success("删除成功");
    }
}
